package com.animal.service.impl;

import com.animal.entity.AnComment;
import com.animal.entity.AnInvitation;
import com.animal.entity.AnOrder;
import com.animal.entity.AnUser;
import com.animal.mapper.AnAnimalMapper;
import com.animal.mapper.AnInvitationMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.service.AnInvitationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author animal
 * @since 2022-01-29
 */
@Service
public class AnInvitationServiceImpl extends ServiceImpl<AnInvitationMapper, AnInvitation> implements AnInvitationService {
    @Autowired
    AnInvitationMapper anInvitationMapper;
    @Autowired
    AnAnimalMapper anAnimalMapper;
    @Autowired
    AnUserMapper anUserMapper;

    @Override
    public List<AnInvitation> findAll(int size ,int current) {
        Page<AnInvitation> userPages = new Page<>(current,size);
        Page<AnInvitation> userPagess=  anInvitationMapper.selectPage(userPages,null);
        List<AnInvitation> list=userPagess.getRecords();
        for(AnInvitation a:list){
            a.setAnUser(anUserMapper.selectById(a.getUserId()));
            if (a.getAnId()!=null){
                a.setAnimal(anAnimalMapper.selectById(a.getAnId()));
            }
        }
        return list;
    }

    @Override
    public List<AnInvitation> findByUserId(int size, int current, int userId) {
        Page<AnInvitation> userPages = new Page<>(size,current);
        QueryWrapper<AnInvitation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Page<AnInvitation> userPagess=  anInvitationMapper.selectPage(userPages,wrapper);
        List<AnInvitation> list=userPagess.getRecords();

        for(AnInvitation a:list){
            a.setAnUser(anUserMapper.selectById(a.getUserId()));
            if (a.getAnId()!=null){
                a.setAnimal(anAnimalMapper.selectById(a.getAnId()));
            }
        }
        return list;
    }

    @Override
    public AnInvitation findById(int inId) {
        AnInvitation anInvitation=anInvitationMapper.selectById(inId);
        anInvitation.setAnUser(anUserMapper.selectById(anInvitation.getUserId()));
        if(anInvitation.getAnId()>0){
            anInvitation.setAnimal(anAnimalMapper.selectById(anInvitation.getAnId()));
        }
        return anInvitation;
    }
}
